package Views;

import Controllers.CustomerCTR;
import Models.*;
import javax.swing.JOptionPane;

/**
 * @author Tanja
 */
public class CustomerInfoView extends javax.swing.JFrame
{
	private Customer customer;
	private CustomerCTR controller = new CustomerCTR();

    public CustomerInfoView(Customer customer)
	{
		this.customer = customer;
        initComponents();
		populateFields();
    }

	private void populateFields()
	{
		nameTF.setText(byggemarked.Tools.getNameOrCompanyName(this.customer));
		addressTF.setText(this.customer.getAddress());
		zipCodeSpinner.setValue(this.customer.getZipCode());
		cityTF.setText(this.customer.getCity());
		phoneNoSpinner.setValue(this.customer.getPhoneNo());
		if(this.customer instanceof BusinessCustomer)
		{
			BusinessCustomer bCustomer = (BusinessCustomer)this.customer;
			cvrSpinner.setValue(bCustomer.getCvr());
			contactTF.setText(bCustomer.getContact());
		}
		activatedCB.setSelected(this.customer.isActivated());
		if(!this.customer.isActivated())
		{
			removeButton.setEnabled(false);
		}
	}

    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        descriptionLabel = new javax.swing.JLabel();
        activatedCB = new javax.swing.JCheckBox();
        nameLabel = new javax.swing.JLabel();
        nameTF = new javax.swing.JTextField();
        addressLabel = new javax.swing.JLabel();
        addressTF = new javax.swing.JTextField();
        zipCodeLabel = new javax.swing.JLabel();
        zipCodeSpinner = new javax.swing.JSpinner();
        cityLabel = new javax.swing.JLabel();
        cityTF = new javax.swing.JTextField();
        phoneNoLabel = new javax.swing.JLabel();
        phoneNoSpinner = new javax.swing.JSpinner();
        cvrLabel = new javax.swing.JLabel();
        cvrSpinner = new javax.swing.JSpinner();
        contactLabel = new javax.swing.JLabel();
        contactTF = new javax.swing.JTextField();
        editButton = new javax.swing.JButton();
        removeButton = new javax.swing.JButton();
        closeButton = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Kundeinformation");

        descriptionLabel.setText("Her ses information om den valgte kunde");

        activatedCB.setSelected(true);
        activatedCB.setText("Aktiveret");
        activatedCB.setEnabled(false);

        nameLabel.setText("Navn eller firmanavn:");

        nameTF.setEnabled(false);

        addressLabel.setText("Adresse:");

        addressTF.setEnabled(false);

        zipCodeLabel.setText("Postnummer:");

        zipCodeSpinner.setEnabled(false);

        cityLabel.setText("By:");

        cityTF.setEnabled(false);

        phoneNoLabel.setText("Telefonnummer:");

        phoneNoSpinner.setEnabled(false);

        cvrLabel.setText("CVR:");

        cvrSpinner.setEnabled(false);

        contactLabel.setText("Kontaktperson:");

        contactTF.setEnabled(false);

        editButton.setText("Rediger");
        editButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                editButtonActionPerformed(evt);
            }
        });

        removeButton.setText("Slet");
        removeButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                removeButtonActionPerformed(evt);
            }
        });

        closeButton.setText("Luk");
        closeButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                closeButtonActionPerformed(evt);
            }
        });

        org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(layout.createSequentialGroup()
                .addContainerGap()
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(layout.createSequentialGroup()
                        .add(descriptionLabel)
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED, 78, Short.MAX_VALUE)
                        .add(activatedCB))
                    .add(layout.createSequentialGroup()
                        .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                            .add(nameLabel)
                            .add(addressLabel)
                            .add(zipCodeLabel)
                            .add(phoneNoLabel)
                            .add(cvrLabel)
                            .add(contactLabel))
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                        .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING, false)
                            .add(layout.createSequentialGroup()
                                .add(zipCodeSpinner, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 74, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                                .add(cityLabel)
                                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                                .add(cityTF, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 169, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                            .add(nameTF)
                            .add(addressTF)
                            .add(phoneNoSpinner, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 137, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                            .add(cvrSpinner, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 148, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                            .add(contactTF)))
                    .add(layout.createSequentialGroup()
                        .add(editButton)
                        .add(18, 18, 18)
                        .add(removeButton)
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED, 169, Short.MAX_VALUE)
                        .add(closeButton)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(layout.createSequentialGroup()
                .addContainerGap()
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(descriptionLabel)
                    .add(activatedCB))
                .add(11, 11, 11)
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(nameLabel)
                    .add(nameTF, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.UNRELATED)
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(addressLabel)
                    .add(addressTF, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.UNRELATED)
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(zipCodeLabel)
                    .add(cityLabel)
                    .add(zipCodeSpinner, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                    .add(cityTF, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.UNRELATED)
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(phoneNoLabel)
                    .add(phoneNoSpinner, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.UNRELATED)
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(cvrLabel)
                    .add(cvrSpinner, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.UNRELATED)
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(contactLabel)
                    .add(contactTF, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                .add(27, 27, 27)
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(editButton)
                    .add(removeButton)
                    .add(closeButton))
                .addContainerGap(org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

	private void editButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_editButtonActionPerformed
		if (editButton.getText().equals("Rediger"))
		{
			nameTF.setEnabled(true);
			addressTF.setEnabled(true);
			zipCodeSpinner.setEnabled(true);
			cityTF.setEnabled(true);
			phoneNoSpinner.setEnabled(true);
			if(this.customer instanceof BusinessCustomer)
			{
				cvrSpinner.setEnabled(true);
				contactTF.setEnabled(true);
			}
			editButton.setText("Gem");
			closeButton.setText("Annuller");
		}
		else if (editButton.getText().equals("Gem"))
		{
			this.customer.setAddress(addressTF.getText());
			this.customer.setZipCode(Integer.parseInt(zipCodeSpinner.getValue().toString()));
			this.customer.setCity(cityTF.getText());
			this.customer.setPhoneNo(Integer.parseInt(phoneNoSpinner.getValue().toString()));
			if(customer instanceof PrivateCustomer)
			{
				PrivateCustomer pCustomer = (PrivateCustomer)customer;
				pCustomer.setName(nameTF.getText());
			}
			else
			{
				BusinessCustomer bCustomer = (BusinessCustomer)customer;
				bCustomer.setCompanyName(nameTF.getText());
				bCustomer.setCvr(Integer.parseInt(cvrSpinner.getValue().toString()));
				bCustomer.setContact(contactTF.getText());
			}
			nameTF.setEnabled(false);
			addressTF.setEnabled(false);
			zipCodeSpinner.setEnabled(false);
			cityTF.setEnabled(false);
			phoneNoSpinner.setEnabled(false);
			if(this.customer instanceof BusinessCustomer)
			{
				cvrSpinner.setEnabled(false);
				contactTF.setEnabled(false);
			}
			editButton.setText("Rediger");
			closeButton.setText("Luk");
		}
	}//GEN-LAST:event_editButtonActionPerformed
	
	private void removeButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_removeButtonActionPerformed
		int choice = JOptionPane.showConfirmDialog(this, "Er du sikker på at du vil slette "+byggemarked.Tools.getNameOrCompanyName(this.customer)+"?", "Bekræftelse af sletning", JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE);
		if(choice == 0)
		{
			this.controller.removeCustomer(customer);
			this.dispose();
		}
	}//GEN-LAST:event_removeButtonActionPerformed

	private void closeButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_closeButtonActionPerformed
		this.dispose();
	}//GEN-LAST:event_closeButtonActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JCheckBox activatedCB;
    private javax.swing.JLabel addressLabel;
    private javax.swing.JTextField addressTF;
    private javax.swing.JLabel cityLabel;
    private javax.swing.JTextField cityTF;
    private javax.swing.JButton closeButton;
    private javax.swing.JLabel contactLabel;
    private javax.swing.JTextField contactTF;
    private javax.swing.JLabel cvrLabel;
    private javax.swing.JSpinner cvrSpinner;
    private javax.swing.JLabel descriptionLabel;
    private javax.swing.JButton editButton;
    private javax.swing.JLabel nameLabel;
    private javax.swing.JTextField nameTF;
    private javax.swing.JLabel phoneNoLabel;
    private javax.swing.JSpinner phoneNoSpinner;
    private javax.swing.JButton removeButton;
    private javax.swing.JLabel zipCodeLabel;
    private javax.swing.JSpinner zipCodeSpinner;
    // End of variables declaration//GEN-END:variables

}
